#include <QCoreApplication>
#include<iostream>
using namespace std;
class BaseCompent;
class  Mediator
{
public:
    virtual void Notify(BaseCompent *sender , const std::string event)=0;
};

class BaseCompent
{
public:
    BaseCompent(Mediator* _M = nullptr){M = _M;}
    void set_mediator(Mediator *_M)
    {
        M = _M;
    }
protected:
    Mediator *M;
};
class comA:public BaseCompent
{
public:
    void seta()
    {

        M->Notify(this,"aaaa");
    }
};

class comB:public BaseCompent
{
public:
    void setb()
    {
        M->Notify(this,"bbbbbbbbbbb");
    }
};
class ConcreteMediator : public Mediator {
public:
    comB *B;
    comA *A;
public:
    ConcreteMediator(comB *_B,comA *_A)
    {
        B= _B;
        A= _A;
        B->set_mediator(this);
        A->set_mediator(this);
    }
    void Notify(BaseCompent *sender , const std::string event)
    {
        if(sender == B)
        {
            std::cout<<"reciveA:"<<event<<std::endl;
            A->seta();
        }
        else
        {
            std::cout<<"reciveB:"<<event<<std::endl;
//            B->setb();
        }
    }
};

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    comA *A = new comA;
    comB *B = new comB;
    ConcreteMediator *M = new ConcreteMediator(B,A);
    A->seta();

    return a.exec();
}

